home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000003_icon-group-sender _Fri Jan 3 09:28:05 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Received: by cheltenham.cs.arizona.edu; Fri, 3 Jan 1997 17:23:33 MST
  2. Date: Fri, 3 Jan 1997 09:28:05 -0600
  3. Message-Id: <97010309280532@cowboy.biomed.com>
  4. From: graham@cowboy.biomed.com (Steve Graham, extension 5224)
  5. To: icon-group@cs.arizona.edu
  6. Subject: Re: Help for an Icon Neophyte
  7. X-Vms-To: SMTP%"icon-group@cs.arizona.edu"
  8. X-Vms-Cc: GRAHAM
  9. Errors-To: icon-group-errors@cs.arizona.edu
  10. Status: RO
  11. Content-Length: 3430
  12.  
  13. Stuart Robinson wrote:
  14.  
  15. >> Hello, there.
  16. >> 
  17. >> In my last posting, I put out an APB on Corre's book, Icon Programming
  18. >> for Humanists, and received many helpful responses.  Thanks.  I finally
  19. >> managed to lay my hands on the book.
  20. >> 
  21. >> Now that I have started teaching myself Icon, I sometimes find that I
  22. >> reach dead-ends, and there is no one around to steer me in the right
  23. >> direction.  So, I thought that I would post problem programs to this
  24. >> newsgroup and hope that some kind soul with more knowledge of Icon would
  25. >> step in and lend a hand.  I hope this is appropriate.
  26. >> 
  27. >> I am trying to write a program to test Zipf's Law.  Zipf was a linguist
  28. >> who observed that when you take the frequency of the words in a text and
  29. >> rank them from most to least frequent that you can then create a log-log
  30. >> plot of frequency against rank and obtain roughly a straight line.
  31. >> 
  32. >> The program that I have created thus far is taken almost directly from
  33. >> The Icon Programming Language.  Here it is:
  34. >> 
  35. >> procedure main()
  36. >> 
  37. >> num := 0
  38. >> 
  39. >> wlist := sort(countwords(), 4)
  40. >>     while num +:= 1 do {
  41. >>     write(left(num||" "||get(wlist), 12), right(get(wlist), 4))
  42. >>     }
  43. >> end
  44. >> 
  45. >> procedure countwords()
  46. >> 
  47. >> wordcount := table(0)
  48. >> 
  49. >> while line := read() do
  50. >>     line ? {
  51. >>                     while tab(upto(&letters)) do
  52. >>                             wordcount[tab(many(&letters))] +:= 1
  53. >>                     }
  54. >> 
  55. >> return wordcount
  56. >> 
  57. >> end
  58. >> 
  59. >> I would like to the program to take a text, convert all of the letters
  60. >> to lowercase (so that "Which" and "which" are not counted as separate
  61. >> words), create a table of all of the words along with their associated
  62. >> frequencies, and to print out the results in ranked fashion.  For
  63. >> example, given the following text
  64. >> 
  65. >> Which is the dog which can outrace the cars?
  66. >> 
  67. >> I would like output more or less as follows (please ignore formatting--I
  68. >> will eventually use entab() for ease of exportation into a program such
  69. >> as Excel):
  70. >> 
  71. >> 1 the 2
  72. >> 2 which 2
  73. >> 3 can 1
  74. >> 4 cars 1
  75. >> 5 dog 1
  76. >> 6 is 1
  77. >> 7 outrace 1
  78. >> 
  79. >> I have run into the following problems.  First, I was unsuccessful at
  80. >> mapping lowercase letters on to uppercase letters.  I assume that I want
  81. >> to use something like map(line, &ucase, &lcase), but where should I
  82. >> insert it?  And should the csets &ucase and &lcase be bracketted by
  83. >> single quotations marks?  Second, the program seems to hang up at the
  84. >> end.  I have no idea why that's happening.
  85. >> 
  86. >> Eventually I would like to have the program do the log-log plot, but for
  87. >> now I can use the resulting output for exportation.
  88. >> 
  89. >> Thanks in advance for any help you can provide.
  90. >> 
  91. >> Stuart Robinson
  92. >> srobinso@reed.edu, robinstu@ohsu.edu
  93. >> 
  94.  
  95. To fix your problem with lowercase, change the following:
  96.  
  97. while line := read() do
  98.     line ? {
  99.                     while tab(upto(&letters)) do
  100.                             wordcount[tab(many(&letters))] +:= 1
  101.                     }
  102.  
  103. to:
  104.  
  105. while (line := read()) do {
  106.     line ? {
  107.                     while tab(upto(&letters)) do {
  108.                             word := map(tab(many(&letters)),&ucase,&lcase)                           
  109.                             wordcount[word] +:= 1
  110.                          }
  111.             }
  112.    }   
  113.  
  114.    I believe that this will also fix the hang up problem.
  115.  
  116. Steve Graham
  117. graham@cowboy.biomed.com
  118.